//
//  VCPath.m
//  LCExpress
//
//  Created by mac1 on 15/10/21.
//  Copyright © 2015年 D0. All rights reserved.
//

#import "VCPath.h"

#import <BaiduMapAPI_Map/BMKMapView.h>

#import <BaiduMapAPI_Location/BMKLocationComponent.h>

@interface VCPath ()<BMKLocationServiceDelegate, BMKMapViewDelegate>

@property (nonatomic, strong) BMKMapView* mapView;

@property (nonatomic, strong) BMKLocationService * mapLocation;

@end

@implementation VCPath

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setNavigation];
    
    [self addMap];
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 添加地图
-(void)addMap
{

    self.mapView = [[BMKMapView alloc]initWithFrame:self.view.frame ];

    self.view = self.mapView;
  
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(113.341642, 23.138856) animated:YES];
    
    self.mapLocation  = [[BMKLocationService alloc]init];
    
    self.mapLocation.delegate = self;
    
    [self.mapLocation startUserLocationService];
    
    self.mapView.showsUserLocation = YES;//显示定位图层
    
    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;
    
}


#pragma  mark 导航栏
-(void)setNavigation
{
    
    [self.navigationController.navigationBar setBarTintColor : [UIColor colorWithRed:2/255.0 green:147/255.0 blue:95/255.0 alpha:1]];

    //创建 标题LABEL
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 140, 20)];
    
    titleLabel.text = @"路线规划";
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    titleLabel.font = [UIFont systemFontOfSize:14];
    
    titleLabel.font = [UIFont boldSystemFontOfSize:20];//加粗
    
    titleLabel.textColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = titleLabel;


    //创建左侧按钮,这样写代码重用性较高
    UIButton *leftButton  = [UIButton buttonWithType:UIButtonTypeSystem];

    leftButton.frame = CGRectMake(0, 0, 30, 30);

    [leftButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];

    [leftButton addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftButton];

    self.navigationItem.leftBarButtonItem = leftBarBtn;


    //设置模糊,默认YES
    [self.navigationController.navigationBar setTranslucent:NO];

}


-(void)leftBtnClick:(UIButton*)leftBtn
{

    [self.navigationController popViewControllerAnimated:YES];

}


#pragma mark 地图回调
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{

    NSLog(@"heading is %@",userLocation.heading);
    
}


//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{

    [self.mapView updateLocationData:userLocation];

    NSLog(@"didUpdateUserLocation lat %f,long %f", userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

}


-(void)viewWillAppear:(BOOL)animated
{

    [self.mapView viewWillAppear];

    self.mapView.delegate = self;

}


-(void)viewWillDisappear:(BOOL)animated
{

    [self.mapView viewWillDisappear];

    self.mapView.delegate = nil;

}


@end
